<template>
  <el-card shadow="hover" class="mt-20">
    <el-form :data="attrFormDate">
      <el-form-item label="属性名">
        <el-input
          placeholder="请输入属性名"
          style="width: 200px"
          v-model="attrFormDate.attrName"
        />
      </el-form-item>
      <el-form-item>
        <template v-slot="{ row }">
          <el-button
            type="primary"
            :disabled="!attrFormDate.attrName"
            @click="addAttr"
            >添加属性值</el-button
          >
        </template>
      </el-form-item>
      <el-form-item>
        <el-table border :data="attrFormDate.attrValueList">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          ></el-table-column>
          <el-table-column label="属性值名称">
            <template v-slot="{ row, $index }">
              <el-input
                ref="inputRef"
                v-if="row.showInput"
                v-model="row.valueName"
                @blur="isShowInput(row, $index)"
              />
              <div v-else @click="showInput(row)">{{ row.valueName }}</div>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template #default="{ row, $index }">
              <el-button
                type="danger"
                :icon="Delete"
                title="删除"
                size="small"
                @click="deleteAttrValue($index)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <!-- 通过input框的值控制按钮的状态 -->
        <el-button type="primary" @click="addOrEditAttr" :disabled="cancelShow">
          保存</el-button
        >
        <el-button @click="cancel">取消</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script lang="ts" setup>
defineOptions({
  name: "AddOrEdit",
});
import { Delete } from "@element-plus/icons-vue";
import { ref, reactive, nextTick, computed } from "vue";
import {
  AttrListItem,
  AttrValueItem,
} from "@/api/product/models/getCategoryType.ts";

import { reqAddOrEditAttr } from "@/api/product/getCategory.ts";
import { useCategoryStore } from "@/stores/category";
//接收数据展示
import { useAttrStore } from "@/stores/attr";
const category = useCategoryStore();
const attrStore = useAttrStore();
//4、保存，发送请求，实现添加和编辑功能
const addOrEditAttr = async () => {
  //有id 就发送编辑请求，没id就添加请求
  await reqAddOrEditAttr({
    ...attrFormDate,
    id: attrStore.id,
    categoryId: category.category3Id,
    categoryLevel: 3,
  });
  ElMessage.success(`${attrStore.id ? "编辑" : "添加"}成功`);
  cancel();
};

//控制保存按钮的状态
const cancelShow = computed(() => {
  return !(
    attrFormDate.attrName &&
    attrFormDate.attrValueList.length > 0 &&
    attrFormDate.attrValueList.some((item) => item.valueName)
  );
});

//3、删除操作
const deleteAttrValue = (index: number) => {
  attrFormDate.attrValueList.splice(index, 1);
};

//2、添加功能
//获取表单数据
const attrFormDate = reactive<AttrListItem>({
  attrName: attrStore.attrName,
  attrValueList: attrStore.attrValueList,
});

//获取真实Dom, 自动获取焦点
const inputRef = ref();
//点击添加属性值,table增加数据
const addAttr = async () => {
  attrFormDate.attrValueList.push({
    valueName: "",
    showInput: true,
  });
  //获取Dom是异步操作，要等待真实input渲染再执行
  await nextTick();
  inputRef.value.focus();
};

//失去焦点时，input框消失， 如果输入框没有值，就撤回这一行
const isShowInput = (row: AttrValueItem, index: number) => {
  if (!row.valueName) {
    //删除该项索引
    attrFormDate.attrValueList.splice(index, 1);
  }
  //input框隐藏
  row.showInput = false;
};

//点击文本框，显示input框
const showInput = async (row: AttrValueItem) => {
  row.showInput = true;
  //获取Dom是异步操作，要等待真实input渲染再执行
  await nextTick();
  inputRef.value.focus();
};

//触发自定义事件
const emit = defineEmits(["cancelShow"]);
//1、点取消按钮触发事件
const cancel = () => {
  emit("cancelShow", false);
};
</script>

<style scoped lang="scss"></style>
